package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatTopic;
import com.happy.cat.project.system.service.ICatTopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 话题Controller
 *
 * @author verne
 * @date 2020-07-22
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/topic" )
@Api("话题 API")
public class CatTopicController extends BaseController {

    private final ICatTopicService iCatTopicService;

    /**
     * 查询话题列表
     */
    @ApiOperation(value = "查询话题列表",notes = "查询话题列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:topic:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatTopic catTopic)
    {
        startPage();
        LambdaQueryWrapper<CatTopic> lqw = new LambdaQueryWrapper<CatTopic>();
        if (StringUtils.isNotBlank(catTopic.getName())){
            lqw.like(CatTopic::getName ,catTopic.getName());
        }
        if (StringUtils.isNotBlank(catTopic.getCoverImg())){
            lqw.eq(CatTopic::getCoverImg ,catTopic.getCoverImg());
        }
        if (catTopic.getStatus() != null){
            lqw.eq(CatTopic::getStatus ,catTopic.getStatus());
        }
        List<CatTopic> list = iCatTopicService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出话题列表
     */
    @ApiOperation(value = "导出话题列表",notes = "导出话题列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:topic:export')" )
    @Log(title = "话题" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatTopic catTopic) {
        LambdaQueryWrapper<CatTopic> lqw = new LambdaQueryWrapper<CatTopic>(catTopic);
        List<CatTopic> list = iCatTopicService.list(lqw);
        ExcelUtil<CatTopic> util = new ExcelUtil<CatTopic>(CatTopic. class);
        return util.exportExcel(list, "topic" );
    }

    /**
     * 获取话题详细信息
     */
    @ApiOperation(value = "获取话题详细信息",notes = "获取话题详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:topic:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatTopicService.getById(id));
    }

    /**
     * 新增话题
     */
    @ApiOperation(value = "新增话题",notes = "新增话题",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:topic:add')" )
    @Log(title = "话题" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatTopic catTopic) {

        catTopic.setCreateBy(SecurityUtils.getUsername());
        catTopic.setCreateTime(new Date());
        return toAjax(iCatTopicService.save(catTopic) ? 1 : 0);
    }

    /**
     * 修改话题
     */
    @ApiOperation(value = "修改话题",notes = "修改话题",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:topic:edit')" )
    @Log(title = "话题" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatTopic catTopic) {
        return toAjax(iCatTopicService.updateById(catTopic) ? 1 : 0);
    }

    /**
     * 删除话题
     */
    @ApiOperation(value = "删除话题",notes = "删除话题",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:topic:remove')" )
    @Log(title = "话题" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatTopicService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
